package com.example.demoqrcode;

import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.example.democommon.contasant.CommonConstant;
import com.example.demoqrcode.service.DemoQrCodeService;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;

/**
 * @version 1.0.0
 * @className: IndexController
 * @description:
 * @author: zhangjunfa
 * @date: 2022/6/14 9:10
 */
@RestController
public class QrCodeController {

    @Autowired
    private DemoQrCodeService demoQrCodeService;

    /**
     * 获取二维码，输出流
     */
    @GetMapping(value = "/getQrCode")
    public void getQrCode(HttpServletResponse response) throws IOException {
        //获取二维码的byte类型的png图片
        byte[] image = demoQrCodeService.getQrCode(CommonConstant.QRCODE_CONTENT);

        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType(CommonConstant.CONTENT_TYPE);
        ServletOutputStream sout = response.getOutputStream();
        sout.write(image);
        sout.flush();
        sout.close();
    }

    /**
     * 生成有logo的二维码图片
     *
     * @param content  二维码中的内容
     * @param logoUrl  二维码中的logo路径(http://127.0.0.1:8085/img/header.png)
     * @param response
     * @throws IOException
     */
    @GetMapping(value = "/getOrgCodeByLogo")
    public void getOrgCodeByLogo(@RequestParam(name = "content") String content,
                                 @RequestParam(name = "logoUrl") String logoUrl,
                                 HttpServletResponse response) throws IOException {
        //获取二维码的byte类型的png图片
        byte[] image = demoQrCodeService.getOrgCodeByLogo(content, logoUrl);

        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType(CommonConstant.CONTENT_TYPE);
        ServletOutputStream sout = response.getOutputStream();
        sout.write(image);
        sout.flush();
        sout.close();
    }

    /**
     * 生成base64的二维码图片
     *
     * @param content  二维码中的内容
     * @throws IOException
     */
    @GetMapping(value = "/getOrgCodeByBase64")
    public String getOrgCodeByBase64(@RequestParam(name = "content") String content) {
        return demoQrCodeService.getOrgCodeBase64(content);
    }


}
